//
//  HJInformationFollowCell.m
//  KaiChat
//
//  Created by lx on 2024/3/12.
//

#import "HJInformationFollowCell.h"
#import "QMUIButton.h"


@implementation HJFollowCell


- (void)settingSubView {
    [super settingSubView];
        
    UIImageView *avatarImageView = [UIImageView addToView:self];
    avatarImageView.addCornerRadius(20);
    avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.centerY.offset(0);
        make.width.height.offset(40);
    }];
    
    UILabel *nameLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(14) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarImageView.mas_right).offset(10);
        make.top.equalTo(avatarImageView).offset(4);
        make.right.mas_lessThanOrEqualTo(-86);
    }];

    UIButton *followButton = [UIButton buttonWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.whiteColor title:@"Follow"];
    followButton.backgroundColor = UIColor.tj_mainColor;
    [followButton setTitle:@"Followed" forState:UIControlStateSelected];
    followButton.addCornerRadius(10);
    [followButton addTarget:self action:@selector(didClickFollowButton) forControlEvents:UIControlEventTouchUpInside];
    [followButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(80);
        make.height.offset(30);
        make.right.inset(leftRightMargin);
        make.centerY.offset(0);
    }];
    
    UILabel *introLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:nil textAlignment:NSTextAlignmentLeft];
    [introLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.bottom.equalTo(avatarImageView).inset(2);
        make.right.mas_lessThanOrEqualTo(followButton.mas_left);
    }];

    _avatarImageView = avatarImageView;
    _nameLabel = nameLabel;
    _introLabel = introLabel;
    _followButton = followButton;
}

- (void)didClickFollowButton {
    if (self.didClickFollowButtonBlock) {
        self.didClickFollowButtonBlock();
    }
}

- (void)setModel:(HJFollowModel *)model {
    _model = model;
    
    [_avatarImageView tj_setImageWithUrl:model.avatar placeholderImage:[UIImage imageNamed:defaultAvatarName]];
    _nameLabel.text = model.nickname;
    _introLabel.text = model.intro;
    
    NSLog(@"model = %@", model);
    
    if (model.isFollow) {
        _followButton.selected = YES;
        _followButton.backgroundColor = UIColor.tj_F6F6F6Color;
        [_followButton setTitleColor:UIColor.tj_BEBDCAColor forState:UIControlStateNormal];
    } else {
        _followButton.selected = NO;
        _followButton.backgroundColor = UIColor.tj_mainColor;
        [_followButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    }
}


@end



@interface HJInformationFollowCell ()<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) QMUIButton *foldButton;

@end

@implementation HJInformationFollowCell

- (void)settingSubView {
    [super settingSubView];
    
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    UIView *headerView = [UIView addToView:self.contentView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(50);
    }];
    
    UILabel *titleLabel = [UILabel lableWithSuperView:headerView font:UIFontMediumMake(16) color:UIColor.tj_333333Color title:@"Who to follow" textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.centerY.offset(0);
    }];
    
    QMUIButton *foldButton = [QMUIButton buttonWithSuperView:headerView font:UIFontMake(12) color:UIColor.tj_mainColor title:nil];
    [foldButton setImagePosition:QMUIButtonImagePositionRight];
    [foldButton setImage:[UIImage imageNamed:@"message_top"] forState:UIControlStateNormal];
    [foldButton setImage:[UIImage imageNamed:@"message_down"] forState:UIControlStateSelected];
    [foldButton setTitle:@"Conceal" forState:UIControlStateNormal];
    [foldButton setTitle:@"All" forState:UIControlStateSelected];
    [foldButton setSpacingBetweenImageAndTitle:4];
    [foldButton addTarget:self action:@selector(didClickFoldButton:) forControlEvents:UIControlEventTouchUpInside];
    _foldButton = foldButton;
    [foldButton sizeToFit];
    [foldButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    CGSize buttonSize = foldButton.bounds.size;
    [foldButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(leftRightMargin);
        make.centerY.offset(0);
        make.width.offset(buttonSize.width + 10);
        make.height.offset(30);
    }];
    
    [self.tableView registerClass:HJFollowCell.class forCellReuseIdentifier:@"HJFollowCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView setSectionIndexColor:UIColor.tj_9B9B9BColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    [self.contentView addSubview:self.tableView];
    self.tableView.rowHeight = 60;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(50);
        make.left.right.bottom.offset(0);
    }];
    
    UIView *lineView = [UIView addToView:self.contentView];
    lineView.backgroundColor = UIColor.tj_separatorColor;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.height.offset(0.7);
        make.bottom.offset(0);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.layout.isFold ? 0 : self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJFollowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJFollowCell" forIndexPath:indexPath];
    HJFollowModel *model = self.datas[indexPath.row];
    TJWeakify(self)
    TJWeakify(cell)
    [cell setDidClickFollowButtonBlock:^{
        if ([weakself.delegate respondsToSelector:@selector(informationCellDidClickFollowButton:followCell:)]) {
            [weakself.delegate informationCellDidClickFollowButton:weakself followCell:weakcell];
        }
    }];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HJFollowCell *followCell = [tableView cellForRowAtIndexPath:indexPath];
    if ([self.delegate respondsToSelector:@selector(informationCellDidClickFollowCell:followCell:indexPath:)]) {
        [self.delegate informationCellDidClickFollowCell:self followCell:followCell indexPath:indexPath];;
    }
}
- (void)didClickFoldButton:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(informationCellDidClickFoldButton:foldButton:)]) {
        [self.delegate informationCellDidClickFoldButton:self foldButton:button];
    }
}

- (void)setLayout:(HJInformationLayout *)layout {
    _layout = layout;
    
    self.foldButton.selected = layout.isFold;
    self.datas = layout.followDatas;
    [self.tableView reloadData];
}
 
- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] init];
        tableView.separatorColor = [UIColor tj_separatorColor];
        tableView.backgroundColor = [UIColor tj_backgroundColor];
        tableView.tableFooterView = [[UIView alloc] init];
        tableView.showsHorizontalScrollIndicator = NO;
        tableView.dataSource = self;
        tableView.delegate = self;
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            tableView.estimatedSectionHeaderHeight = 0;
            tableView.estimatedSectionFooterHeight = 0;
            tableView.estimatedRowHeight = 0;
        }
        
        if (@available(iOS 15.0, *)) {
            tableView.sectionHeaderTopPadding = 0;
        }
        
        _tableView = tableView;
    }
    return _tableView;
}
 
@end
